package com.sdppf.action;

import java.net.URLDecoder;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sdppf.domain.Developer;
import com.sdppf.service.DeveloperService;

public class DeveloperAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private DeveloperService developerService;
	private List<Developer> developers;
	private int depId;
	private String developerNum;
	private String developerName;
	private String sex;
	private String cellPhone;

	public int getDepId() {
		return depId;
	}

	public void setDepId(int depId) {
		this.depId = depId;
	}

	public String getDeveloperNum() {
		return developerNum;
	}

	public void setDeveloperNum(String developerNum) {
		this.developerNum = developerNum;
	}

	public String getDeveloperName() {
		return developerName;
	}

	public void setDeveloperName(String developerName) {
		this.developerName = developerName;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getCellPhone() {
		return cellPhone;
	}

	public void setCellPhone(String cellPhone) {
		this.cellPhone = cellPhone;
	}

	public List<Developer> getDevelopers() {
		return developers;
	}

	public void setDevelopers(List<Developer> developers) {
		this.developers = developers;
	}

	public DeveloperService getDeveloperService() {
		return developerService;
	}

	public void setDeveloperService(DeveloperService developerService) {
		this.developerService = developerService;
	}

	public String saveDeveloper() throws Exception {
		Developer developer = new Developer();
		developer.setDeveloperNum(URLDecoder.decode(this.developerNum, "UTF-8"));
		developer.setDeveloperName(URLDecoder.decode(this.developerName, "UTF-8"));
		developer.setSex((URLDecoder.decode(this.sex, "UTF-8")));
		developer.setCellPhone(this.cellPhone);
		developer.setPassword("8888");
		System.out.println(developerName);
		this.developerService.saveDeveloper(developer);
		return null;
	}

	public String deleteDeveloper() throws Exception {
		Developer developer = this.developerService.queryDeveloper(this.depId);
		this.developerService.deleteDeveloper(developer);
		return null;
	}

	public String updateDeveloper() throws Exception {
		Developer developer = this.developerService.queryDeveloper(this.depId);
		developer.setDeveloperNum(this.developerNum);
		developer.setDeveloperName(URLDecoder.decode(this.developerName,
				"UTF-8"));
		developer.setSex(URLDecoder.decode(this.sex, "UTF-8"));
		developer.setCellPhone(this.cellPhone);
		this.developerService.updateDeveloper(developer);
		return null;
	}

	public String queryAll() throws Exception {
		this.developers = this.developerService.queryDevelopers();
		return "success";
	}
	
	public String validateDeveloperNum() throws Exception {
		this.developers = this.developerService.queryDevelopers();
		HttpServletResponse response = ServletActionContext.getResponse();
		for(Iterator<Developer> deveIter = this.developers.iterator(); deveIter.hasNext(); ) {
			Developer developer = (Developer)deveIter.next();
			if(this.developerNum.equals(developer.getDeveloperNum())) {
				response.getWriter().write("<font color=\"red\">already used!</font>");
				break;
			} 
		}
		return null;
	}

}
